package cn.bdqn.pn.service.commodity.CommodityServiceImpl;
import cn.bdqn.pn.pojo.commodity.Commodity;
import cn.bdqn.pn.pojo.commodity.params.CommodityParams;
import cn.bdqn.pn.reposity.commodity.CommodityReposity;
import cn.bdqn.pn.service.commodity.CommodityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
@Service("commodityService")
public class CommodityServiceImpl implements CommodityService {
    @Autowired
    CommodityReposity commodityReposity;

    @Override
    public Page<Commodity>  findAllCommodity(CommodityParams params,Integer page,Integer size) {
        Specification<Commodity> specification=new Specification<Commodity>() {
            @Override
            public Predicate toPredicate(Root<Commodity> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cb) {
                List<Predicate> list=new ArrayList<>();
                if (params.getCommodityName()!=null && !params.getCommodityName().equals("")){
                    list.add(cb.like(root.get("commodityName"),"%"+params.getCommodityName()+"%"));
                }
                if (params.getProvinceId()!=null && params.getProvinceId()!=0){
                    list.add(cb.equal(root.get("provinceId"),params.getProvinceId()));
                }
                if (params.getCityId()!=null && params.getCityId()!=0){
                    list.add(cb.equal(root.get("cityId"),params.getCityId()));
                }
                if (params.getTypeId()!=null && params.getTypeId()!=0){
                    list.add(cb.equal(root.get("typeId"),params.getTypeId()));
                }
                if (params.getClildTypeId()!=null && params.getClildTypeId()!=0){
                    list.add(cb.equal(root.get("clildTypeId"),params.getClildTypeId()));
                }
                if (params.getIsSeat()!=null && params.getIsSeat()!=0){
                    list.add(cb.equal(root.get("isSeat"),params.getIsSeat()));
                }
                if (params.getStar()!=null && params.getStar()!=0){
                    list.add(cb.equal(root.get("star"),params.getStar()));
                }
                if (params.getStatus()!=null && params.getStatus()!=-1){
                    list.add(cb.equal(root.get("status"),params.getStatus()));
                }

                if (params.getStartTime()!=null&& !params.getStartTime().equals("") ){//大于
                    list.add(cb.greaterThan(root.get("startTime"),params.getStartTime()));
                }
                if(params.getEndTime() != null && !params.getEndTime().equals("")){
                    list.add(cb.lessThan(root.get("endTime"),params.getEndTime()));
                }
                if (params.getIsDel()!=null && params.getIsDel()!=0){
                    list.add(cb.equal(root.get("isDel"),params.getIsDel()));
                }
                Predicate[] predicates = new Predicate[list.size()];
                return cb.and(list.toArray(predicates));
            }
        };
        Pageable pageable = new PageRequest(page=page==null?0:page,size=size==null?2:size);
        Page<Commodity> commodityPage=commodityReposity.findAll(specification,pageable);
        //List<Commodity> commodityList=commodityReposity.findAll(specification);
        return commodityPage;
    }

    @Override
    public Page<Commodity> getCommodityInfoByPage(Commodity commodity, Integer page, Integer size) {
        Specification<Commodity> specification=new Specification<Commodity>() {
            @Override
            public Predicate toPredicate(Root<Commodity> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cb) {
                List<Predicate> list=new ArrayList<>();
                if (commodity.getCommodityName()!=null && !commodity.getCommodityName().equals("")){
                    System.out.println(commodity.getCommodityName()+" ++++++++++++++++++");
                    list.add(cb.like(root.get("commodityName"),"%"+commodity.getCommodityName()+"%"));
                }
                if (commodity.getProvinceId()!=null && commodity.getProvinceId()!=0){
                    list.add(cb.equal(root.get("provinceId"),commodity.getProvinceId()));
                }
                if (commodity.getCityId()!=null && commodity.getCityId()!=0){
                    list.add(cb.equal(root.get("cityId"),commodity.getCityId()));
                }
                if (commodity.getTypeId()!=null && commodity.getTypeId()!=0){
                    list.add(cb.equal(root.get("typeId"),commodity.getTypeId()));
                }
                if (commodity.getClildTypeId()!=null && commodity.getClildTypeId()!=0){
                    list.add(cb.equal(root.get("clildTypeId"),commodity.getClildTypeId()));
                }
                if (commodity.getIsSeat()!=null && commodity.getIsSeat()!=0){
                    list.add(cb.equal(root.get("isSeat"),commodity.getIsSeat()));
                }
                if (commodity.getStar()!=null && commodity.getStar()!=0){
                    System.out.println("123456+++++++++++++++++++++++++++++++++++++++++++");
                    list.add(cb.equal(root.get("star"),commodity.getStar()));
                }
                if (commodity.getStatus()!=null && commodity.getStatus()!=-1){
                    list.add(cb.equal(root.get("status"),commodity.getStatus()));
                }

                if (commodity.getStartTime()!=null&& !commodity.getStartTime().equals("") ){//大于
                    list.add(cb.greaterThan(root.get("startTime"),commodity.getStartTime()));
                }
                if(commodity.getEndTime() != null && !commodity.getEndTime().equals("")){
                    list.add(cb.lessThan(root.get("endTime"),commodity.getEndTime()));
                }

                    list.add(cb.equal(root.get("isDel"),1));

                Predicate[] predicates = new Predicate[list.size()];
                return cb.and(list.toArray(predicates));
            }
        };
        Pageable pageable = new PageRequest(page=page==null?0:page,size=size==null?2:size);
        Page<Commodity> commodityPage=commodityReposity.findAll(specification,pageable);
        //List<Commodity> commodityList=commodityReposity.findAll(specification);
        return commodityPage;
    }

    /**
     * 更新商品状态
     * 上下架
     * @param commodityId 商品Id
     * @param status 商品要变为的状态
     * @return
     */
    @Override
    public boolean updateCommodityStatus(Integer commodityId,Integer status) {
        Commodity commodity=commodityReposity.findOne(commodityId);
        commodity.setStatus(status);
        Commodity commodity2=commodityReposity.save(commodity);

        if (commodity2!=null) {
            return true;
        }else{
            return false;
        }

    }

    @Override
    /**
     * 删除商品
     */
    public boolean updateCommodity(Integer commodityId) {
        System.out.println("-------------------------------------------------------------------------------"+commodityId);
        Commodity commodity1=commodityReposity.findOne(commodityId);
        commodity1.setIsDel(2);
        Commodity commodity2= commodityReposity.save(commodity1);
        if (commodity2!=null){
            return true;
        }else{
            return false;
        }
    }
    @Override
    /**
     * 添加方法
     */
    public Commodity addCommodity(Commodity commodity) {

        return commodityReposity.save(commodity);
    }


    /**
     * 根据商品Id查询商品信息
     * @param commodityId
     * @return
     */
    @Override
    public Commodity getCommodityById(Integer commodityId) {
        System.out.println("--------------"+commodityId);
        return commodityReposity.findOne(commodityId);
    }

    @Override
    public List<Commodity> findAllCommdity(Integer status) {
        return commodityReposity.findAllByStatus(status);
    }


    @Override
    public Commodity findByCommodityName(String commodityName) {
        return commodityReposity.findAllByCommodityName(commodityName);
    }
    /**
     * 根据输入内容模糊查商品名返回集合
     * @param commodityName
     * @return
     */
    @Override
    public List<Commodity> findCommodityByCommodityNameLike(String commodityName) {
        return commodityReposity.findCommodityByCommodityNameLike(commodityName);
    }


}
